package com.ts.api.module.match.plan.handler;

import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.log.Log;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.ts.api.common.constant.productflow.ProductApplyFlowStatusEnum;
import com.ts.api.common.util.ExceptionUtils;
import com.ts.api.module.api.entity.ApiUserRequest;
import com.ts.api.module.api.service.ApiBizMultiPushService;
import com.ts.api.module.match.plan.config.PlanMatchConfig;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeResult;
import com.ts.api.module.match.plan.internal.selection.PostPlanSelector;
import com.ts.api.module.match.plan.internal.selection.context.PostPlanSelectionType;
import com.ts.api.module.match.plan.model.AbstractPlanMatch;
import com.ts.api.module.match.plan.model.PlanMatchProcess;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.service.ProductApplyFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  计划匹配成功处理器
 *
 *  @author Pan
 */
@Component
public class ChannelPlanMatchProcessSuccessHandler {

    @Autowired
    private PostPlanSelector postPlanSelector;
    @Autowired
    private ProductApplyFlowService productApplyFlowService;
    @Autowired
    private ApiBizMultiPushService apiBizMultiPushService;

    /**
     *  计划匹配成功处理
     *
     *  @param  planMatchProcess        计划匹配处理数据实体
     *  @return ProductPlanPO           返回单个最终匹配计划
     */
    public PlanDistributeResult handleSuccess(PlanMatchProcess planMatchProcess) {
        AbstractPlanMatch abstractPlanMatch = planMatchProcess.getAbstractPlanMatch();
        ApiUserRequest sourceApiUserRequest = planMatchProcess.getSourceApiUserRequest();
        List<PlanDistributeResult> listSucMatchResult = planMatchProcess.getListMatchResult();

        //  计划结果处理-价格优先
        try {
            PlanDistributeResult finalMatchPlan = postPlanSelector.select(listSucMatchResult, PostPlanSelectionType.CHANNEL_PRICE);
            //  插入流程成功日志
            ProductApplyFlowPO currentDistributeFlow = finalMatchPlan.getCurrentDistributeFlow();
            productApplyFlowService.insert(currentDistributeFlow, false);

            //  配置类
            PlanMatchConfig planMatchConfig = abstractPlanMatch.getPlanMatchConfig();

            //  是否允许多推
            if (planMatchConfig.getAllowMultiPush()) {
                apiBizMultiPushService.multiDataExchange(sourceApiUserRequest, finalMatchPlan, listSucMatchResult);
            }
            return finalMatchPlan;
        } catch (Exception e) {
            String stackTrace = ExceptionUtils.getStackTrace(e);
            ProductApplyFlowPO failFlow = CopyUtils.copy(listSucMatchResult.getFirst().getCurrentDistributeFlow(), ProductApplyFlowPO.class);
            failFlow.setExceptionMsg(StringUtils.format("自有渠道-撞库匹配处理时异常:{}", e.getMessage()));
            failFlow.setStackMsg(stackTrace);
            failFlow.setStatus(ProductApplyFlowStatusEnum.DATA_EXCHANGE_EXCEPTION.getType());
            failFlow.setBizStatus("0");
            productApplyFlowService.insert(failFlow, false);
            Log.get().error("{}", e.getMessage(), e);
            return PlanDistributeResult.ofFail("处理匹配计划成功时出现异常:" + e.getMessage());
        }
    }
}
